package net.randomice.emf.observables.demo.extlibrary.items.observables;

import java.util.Date;
import net.randomice.emf.observables.demo.extlibrary.items.Book;
import net.randomice.emf.observables.demo.extlibrary.items.BookCategory;
import net.randomice.emf.observables.demo.extlibrary.items.ItemsFactory;
import net.randomice.emf.observables.demo.extlibrary.items.ItemsPackage;
import net.randomice.emf.observables.demo.extlibrary.persons.Writer;
import net.randomice.emf.observables.demo.extlibrary.persons.observables.WriterOO;
import net.randomice.emf.observables.runtime.IObservableAbstractListObject;
import net.randomice.emf.observables.runtime.IObservableObject;
import org.eclipse.core.databinding.observable.Observables;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * An Observable object wrapper for {@link Book}
 * @generated
 */
public class BookOO implements IObservableObject<Book> {
	// root object stuff

	public final static Creator<Book, BookOO> CREATOR = new Creator<Book, BookOO>() {
		public BookOO createRoot(Book object) {
			return new BookOO(Observables.constantObservableValue(object,
					Book.class));
		}

		public BookOO createRoot() {
			return createRoot(ItemsFactory.eINSTANCE.createBook());
		}
	};

	public static BookOO createRoot(Book object) {
		return CREATOR.createRoot(object);
	}

	public static BookOO createRoot() {
		return CREATOR.createRoot();
	}

	// instance stuff	

	public final IObservableValue o;
	public final IObservableValue getObservable() {
		return o;
	}

	public BookOO(IObservableValue observableValue) {
		Object type = observableValue.getValueType();
		if (type instanceof EReference) {
			type = ((EReference) type).getEType().getInstanceClass();
		}
		Assert.isTrue(null == type || Book.class.equals(type));
		o = observableValue;
	}

	public Book getValue() {
		return (Book) o.getValue();
	}

	public void setValue(Book newValue) {
		o.setValue(newValue);
	}

	// generic feature getters

	public IObservableValue getValueFeature(EStructuralFeature feature) {
		if (ItemsPackage.Literals.ITEM__PUBLICATION_DATE.equals(feature)) {
			return getPublicationDate();
		}
		if (ItemsPackage.Literals.BOOK__TITLE.equals(feature)) {
			return getTitle();
		}
		if (ItemsPackage.Literals.BOOK__PAGES.equals(feature)) {
			return getPages();
		}
		if (ItemsPackage.Literals.BOOK__CATEGORY.equals(feature)) {
			return getCategory();
		}
		if (ItemsPackage.Literals.BOOK__AUTHOR.equals(feature)) {
			return getAuthor().o;
		}
		throw new UnsupportedOperationException("Feature " + feature
				+ " not supported!");
	}

	public IObservableList getListFeature(EStructuralFeature feature) {
		throw new UnsupportedOperationException("Feature " + feature
				+ " not supported!");
	}

	public IObservableObject<?> getValueObject(EReference feature) {
		if (ItemsPackage.Literals.BOOK__AUTHOR.equals(feature)) {
			return getAuthor();
		}
		throw new UnsupportedOperationException("Feature " + feature
				+ " not supported!");
	}

	public IObservableAbstractListObject<?> getListObject(EReference feature) {
		throw new UnsupportedOperationException("Feature " + feature
				+ " not supported!");
	}

	// features from type Item

	private IObservableValue publicationDate = null;
	public IObservableValue getPublicationDate() {
		if (null == publicationDate) {
			publicationDate = EMFObservables.observeDetailValue(o.getRealm(),
					o, ItemsPackage.Literals.ITEM__PUBLICATION_DATE);
		}
		return publicationDate;
	}

	public void setPublicationDate(Date value) {
		getPublicationDate().setValue(value);
	}

	// features from type Book

	private IObservableValue title = null;
	public IObservableValue getTitle() {
		if (null == title) {
			title = EMFObservables.observeDetailValue(o.getRealm(), o,
					ItemsPackage.Literals.BOOK__TITLE);
		}
		return title;
	}

	public void setTitle(String value) {
		getTitle().setValue(value);
	}

	private IObservableValue pages = null;
	public IObservableValue getPages() {
		if (null == pages) {
			pages = EMFObservables.observeDetailValue(o.getRealm(), o,
					ItemsPackage.Literals.BOOK__PAGES);
		}
		return pages;
	}

	public void setPages(int value) {
		getPages().setValue(value);
	}

	private IObservableValue category = null;
	public IObservableValue getCategory() {
		if (null == category) {
			category = EMFObservables.observeDetailValue(o.getRealm(), o,
					ItemsPackage.Literals.BOOK__CATEGORY);
		}
		return category;
	}

	public void setCategory(BookCategory value) {
		getCategory().setValue(value);
	}

	private WriterOO author = null;
	public WriterOO getAuthor() {
		if (null == author) {
			author = new WriterOO(EMFObservables.observeDetailValue(o
					.getRealm(), o, ItemsPackage.Literals.BOOK__AUTHOR));
		}
		return author;
	}

	public void setAuthor(Writer value) {
		getAuthor().setValue(value);
	}

}
